home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / misc / MPackMUI.lha / MPackMUI / Source / Peripheral.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-15  |  6.0 KB  |  194 lines

  1. // --------------------------------------------------------------------------------------------------------------
  2. //
  3. //   MPackMUI V1.01 Peripheral Module
  4. //
  5. // --------------------------------------------------------------------------------------------------------------
  6.  
  7. #include "Peripheral.h"
  8.  
  9. // --------------------------------------------------------------------------------------------------------------
  10.  
  11. BOOL DoAslFileReq(char *buffer, char *wholepath, BOOL save)
  12. {
  13.     struct FileRequester *req;
  14.     char filename[256] = "", directory[256] = "";
  15.     char *filepart;
  16.     struct FileInfoBlock *fib;
  17.     BPTR lock;
  18.  
  19.     // Request a file to open/save to, and store the complete path in the buffer
  20.  
  21.     // N.B. buffer must point to a block of at least 256 bytes of memory
  22.  
  23.     // Extract directory and filename details from path
  24.  
  25.     if ((wholepath[strlen(wholepath) - 1] == ':') || (wholepath[strlen(wholepath) - 1] == '/'))
  26.     {
  27.         // Path only contains a directory (with a leading slash/colon)
  28.  
  29.         strcpy(directory, wholepath);
  30.     } /* if */
  31.  
  32.     else
  33.     {
  34.         // Path contains a directory and/or a filename
  35.  
  36.         if ((lock = Lock(wholepath, ACCESS_READ)) != 0)
  37.         {
  38.             if (!(fib = AllocDosObjectTags(DOS_FIB,                             TAG_DONE)))
  39.             {
  40.                 DoEasyReq("Couldn't allocate FileInfoBlock");
  41.                 UnLock(lock);
  42.                 CleanUp();
  43.             } /* if */
  44.  
  45.             Examine(lock, fib);
  46.  
  47.             if (fib->fib_DirEntryType > 0)
  48.             {
  49.                 // Path contains a directory only
  50.  
  51.                 strcpy(directory, wholepath);
  52.             } /* if */
  53.  
  54.             else
  55.             {
  56.                 // Path contains a directory and/or a filename
  57.  
  58.                 filepart = FilePart(wholepath);
  59.  
  60.                 if (strlen(filepart) == strlen(wholepath))
  61.                 {
  62.                     // Path contains only a filename
  63.  
  64.                     strcpy(filename, wholepath);
  65.                 } /* if */
  66.  
  67.                 else
  68.                 {
  69.                     // Path contains a directory and a filename
  70.  
  71.                     strcpy(filename, filepart);
  72.                     strncpy(directory, wholepath, strlen(wholepath) - strlen(filepart));
  73.                     directory[strlen(wholepath) - strlen(filepart)] = '\0';
  74.                 } /* else */
  75.             } /* else */
  76.  
  77.             FreeDosObject(DOS_FIB, fib);
  78.             UnLock(lock);
  79.         } /* if */
  80.  
  81.         else
  82.         {
  83.             // Path contains either a filename only or a filename and a directory
  84.  
  85.             filepart = FilePart(wholepath);
  86.  
  87.             if (strlen(filepart) == strlen(wholepath))
  88.             {
  89.                 // Path contains only a filename
  90.  
  91.                 strcpy(filename, wholepath);
  92.             } /* if */
  93.  
  94.             else
  95.             {
  96.                 // Path contains a directory and a filename
  97.  
  98.                 strcpy(filename, filepart);
  99.                 strncpy(directory, wholepath, strlen(wholepath) - strlen(filepart));
  100.                 directory[strlen(wholepath) - strlen(filepart)] = '\0';
  101.             } /* else */
  102.         } /* else */
  103.     } /* else */
  104.  
  105.     // Request file
  106.  
  107.     if (!(req = AllocAslRequestTags(ASL_FileRequest,                            TAG_DONE)))
  108.     {
  109.         DoEasyReq("Warning: couldn't allocate ASL requester");
  110.         return(FALSE);
  111.     } /* if */
  112.  
  113.     if (!(AslRequestTags(req,                                                   ASLFR_TitleText, "Select file",
  114.                                                                                 ASLFR_RejectIcons, TRUE,
  115.                                                                                 ASLFR_InitialDrawer, directory,
  116.                                                                                 ASLFR_InitialFile, filename,
  117.                                                                                 ASLFR_DoSaveMode, save,
  118.                                                                                 TAG_DONE)))
  119.     {
  120.         // User cancelled requester
  121.  
  122.         FreeAslRequest(req);
  123.         return(FALSE);
  124.     } /* if */
  125.  
  126.     // Create complete path
  127.  
  128.     strcpy(buffer, req->fr_Drawer);
  129.  
  130.     AddPart(buffer, req->fr_File, 256);
  131.  
  132.     FreeAslRequest(req);
  133.  
  134.     return(TRUE);
  135. } /* DoAslFileReq() */
  136.  
  137. // --------------------------------------------------------------------------------------------------------------
  138.  
  139. BOOL DoAslDirReq(char *buffer, char *path)
  140. {
  141.     struct FileRequester *req;
  142.  
  143.     // Request a directory, and store the path in the buffer
  144.  
  145.     // N.B. buffer must point to a block of at least 256 bytes of memory
  146.  
  147.     if (!(req = AllocAslRequest(ASL_FileRequest,                                TAG_DONE)))
  148.     {
  149.         DoEasyReq("Warning: couldn't allocate ASL requester");
  150.         return(FALSE);
  151.     } /* if */
  152.  
  153.     if (!(AslRequestTags(req,                                                   ASLFR_TitleText, "Select directory",
  154.                                                                                 ASLFR_DrawersOnly, TRUE,
  155.                                                                                 ASLFR_RejectIcons, TRUE,
  156.                                                                                 ASLFR_InitialDrawer, path,
  157.                                                                                 TAG_DONE)))
  158.     {
  159.         // User cancelled requester
  160.  
  161.         FreeAslRequest(req);
  162.         return(FALSE);
  163.     } /* if */
  164.  
  165.     // Create path
  166.  
  167.     strcpy(buffer, req->fr_Drawer);
  168.  
  169.     FreeAslRequest(req);
  170.  
  171.     return(TRUE);
  172. } /* DoAslDirReq() */
  173.  
  174. // --------------------------------------------------------------------------------------------------------------
  175.  
  176. void DoEasyReq(char *message)
  177. {
  178.     // EasyRequest with a message
  179.  
  180.     struct EasyStruct req = {
  181.         sizeof(struct EasyStruct),
  182.         0,
  183.         "MPackMUI V1.01",
  184.         message,
  185.         "OK!"
  186.     };
  187.  
  188.     EasyRequest(NULL, &req, NULL, NULL);
  189. } /* DoEasyReq() */
  190.  
  191. // --------------------------------------------------------------------------------------------------------------
  192.  
  193. // End Of Text
  194.